/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.web.quartz.news;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.crawler.NewsContentImageCrawler;

/**
 * ClassName:CrawlerTimer <br/>
 * Date: 2018年11月27日 下午3:18:42 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Service
public class NewsContentImageCrawlerTimer {

    private static final Logger logger    = Logger.getLogger(NewsContentImageCrawlerTimer.class);
    private AtomicBoolean       isRunning = new AtomicBoolean(false);

    /**
     * 每分钟执行一次
     */
    public void execute() {
        try {
            logger.info("Begin to schedule crawle news content image");
            if (isRunning.get()) {
                logger.warn("The news content image crawler job is still running.");
                return;
            }
            isRunning.set(true);
            List<NewsContentImageCrawler> crawlerList = NewsContentImageCrawler.getNewsContentImageCrawlerList();
            if (crawlerList == null || crawlerList.size() == 0) {
                logger.info("Cannot find any news content image crawler.");
                return;
            }
            crawlerList.forEach((c) -> {
                try {
                    c.getContentImage();
                } catch (Exception e) {
                    logger.error("Crawl content from " + c.getClass().getName() + " exception happened:" + e.getMessage(), e);
                }
            });
            logger.info("Finish schedule crawle news content image");
            
        } catch (Exception e) {
            logger.error("The crawler timer execute error happened:" + e.getMessage(), e);
        } finally {
            isRunning.set(false);
        }
    }
}
